﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;

namespace BrainTechLLC
{
	[Serializable]
	[DataContract]
	public class StateTemplate : Template
	{
		protected string _stateName;
		protected int _baseRescheduleFrequency;
		protected ThreadSafeLookup<string, CommandToPerform> _modifyExecutingState;

		public string StateName { get { return _stateName; } set { _stateName = value; } }
		public int BaseRescheduleFrequency { get { return _baseRescheduleFrequency; } set { _baseRescheduleFrequency = value; } }

		public ThreadSafeLookup<string, CommandToPerform> ModifyExecutingState
		{
			get
			{
				if (_modifyExecutingState == null)
					Interlocked.CompareExchange<ThreadSafeLookup<string, CommandToPerform>>(ref _modifyExecutingState, new ThreadSafeLookup<string, CommandToPerform>(), null);

				return _modifyExecutingState;
			}
			set
			{
				_modifyExecutingState = value;
			}
		}


		public override void RegisterTemplate()
		{
			base.RegisterTemplate();
		}

		public override void AddDefaults(IFromTemplate item)
		{
			base.AddDefaults(item);
			State state = item as State;
			state.BaseRescheduleFrequency = BaseRescheduleFrequency;
			state.StateName = StateName;
			if (_modifyExecutingState != null)
			{
				foreach (var kvp in _modifyExecutingState.AllKeyValuePairs)
				{
					state.ModifyExecutingState.AddOrSet(kvp.Key, kvp.Value);
				}
			}
		}
	}
}
